package com.eshin;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.ContextIdApplicationContextInitializer;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

/**
 * 定义默认的Keyword与值
 *
 * @author eshin
 */
public class DefaultKeywords{
    
    private Map<String, String> defaultMap = new ConcurrentHashMap<>();
    
    @Value("${server.port:8080}")
    private String port;
    @Autowired
    private Environment env;
    
    public Map<String, String> getDefaultKeyValues() {
        return defaultMap;
    }
    
    @PostConstruct
    public void init() {
    	defaultMap.put("PORT", port);
    	
        initIpv4AndHostname();
        
        initApplicationName();
    }
    
    
    private void initIpv4AndHostname() {
        String ip = "0.0.0.0";
        String hostName = "localhost";
        try {
            Enumeration<NetworkInterface> networkInterfaces =
                    NetworkInterface.getNetworkInterfaces();
            boolean findSiteLocalAddress = false;
            while (!findSiteLocalAddress && networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                if (networkInterface.isLoopback() || !networkInterface.isUp() ||
                    networkInterface.isVirtual() || networkInterface.isPointToPoint()) {
                    continue;
                }
                
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress address = inetAddresses.nextElement();
                    if (address instanceof Inet4Address && address.isSiteLocalAddress()) {
                        ip = address.getHostAddress();
                        hostName = address.getHostName();
                        findSiteLocalAddress = true;
                        break;
                    }
                }
            }
        } catch (SocketException e) {
        }
        
        defaultMap.putIfAbsent("IP", ip);
        defaultMap.putIfAbsent("HOSTNAME", hostName);
    }
    
    /**
     * @see ContextIdApplicationContextInitializer
     */
    private void initApplicationName() {
        String applicationName = env.resolvePlaceholders(
                "${spring.application.name:${vcap.application.name:${spring.config" +
                ".name:application}}}");
        if (StringUtils.isEmpty(applicationName)) {
            applicationName = defaultMap.get("HOSTNAME") + "_app";
        }
        defaultMap.put("APPLICATION_NAME", applicationName);
    }
}
